package at.fhooe.engine.pathfinding;

import at.fhooe.engine.event.events.*;
import at.fhooe.engine.pandora.Game;

public class TileMap implements MapObjectCreatedEvent.Listener{

	private int[][] m_map;
	private int m_xZoning;
	private int m_yZoning;
	
	public TileMap(int xZoning, int yZoning)
	{
		m_map = new int[xZoning][yZoning];
		m_xZoning = xZoning;
		m_yZoning = yZoning;
		
		//Game.getInstance().getEventManager().addListener(this, MapObjectCreatedEvent.TYPE);
	}
	
	public int[][] getMap() {
		return m_map;
	}
	
	public int getXZoning() {
		return m_xZoning;
	}
	
	public int getYZoning() {
		return m_yZoning;
	}
	
	public void drawMap()
	{
		for(int j = 0; j < m_yZoning; j++)
		{
			System.out.print("|");
			for(int i = 0; i < m_xZoning; i++)
			{
				//System.out.print(" ");
				if(m_map[i][j] == BalancingPf.LEVEL_WALL)
				{
					System.out.print("X");
				}
				else {
					System.out.print("0");
				}
				//System.out.print(" ");
			}
			System.out.println("|");
		}
	}
	
	public void onMapObjectCreatedEvent(MapObjectCreatedEvent mapObjectCreatedEvent) {
		if(mapObjectCreatedEvent.getMapObjectId().startsWith("Map_"))
		{
			double x = mapObjectCreatedEvent.getPositionX();
			double y = mapObjectCreatedEvent.getPositionY();
			double s = mapObjectCreatedEvent.getSize();
			
			x = x + s / 2.0;
			y = y + s / 2.0;
			int x1 = (int)(x * m_xZoning / (double)Game.getInstance().getGraphicsSystem().getWidth() * 0.7);
			int y1 = (int)(y * m_yZoning / (double)Game.getInstance().getGraphicsSystem().getHeight() * 0.7);
			
			m_map[x1][y1] = BalancingPf.LEVEL_WALL;
		}
	}

	public void allLoaded() {
		Game.getInstance().getEventManager().addListener(this, MapObjectCreatedEvent.TYPE);
	}
}
